package com.bc004346.travelus.Fragment;

import android.app.Activity;
import android.app.ListFragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.bc004346.travelus.Model.Domain.Flight;
import com.bc004346.travelus.R;
import com.bc004346.travelus.Utility;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.bc004346.travelus.Utility.log;

/**
 * Created by Renats on 3/16/14.
 * Flight overview in PBT
 */
public class PbtFlightsFragment extends ListFragment {

    //<editor-fold desc="Members">

    public static final String PBT_TRACKER_TASK_TAG = "pbtTrackerTask";
    private Activity mActivity;
    private OnFlightSelectionChangeListener mListener;
    private TextView mStatus;

    //</editor-fold>
    //<editor-fold desc="Properties">

    public Activity getParentActivity() {
        if (mActivity == null)
            mActivity = getActivity();
        return mActivity;
    }

    public OnFlightSelectionChangeListener getListener() {
        if (mListener == null) {
            mListener = (OnFlightSelectionChangeListener) getActivity();
        }
        return mListener;
    }

    public TextView getStatusTextView() {
        if (mStatus == null) {
            mStatus = (TextView) getParentActivity().findViewById(R.id.statusTextView);
        }
        return mStatus;
    }

    //</editor-fold>
    //<editor-fold desc="Overrides">

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {

        Object item = l.getItemAtPosition(position);
        if (item != null && item.getClass().equals(Flight.class)) {
            Flight flight = (Flight) l.getItemAtPosition(position);
            if (getListener() != null && flight != null)
                getListener().onFlightSelectionChanged(flight);
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        try {
            getFlights();
        } catch (Exception e) {
            log(e, getParentActivity());
        }
    }

    //</editor-fold>
    //<editor-fold desc="Private Methods">

    private void getFlights() {

        new AsyncTask<Void, Void, List<Flight>>() {

            private Exception ex = null;

            @Override
            protected void onPostExecute(List<Flight> f) {

                populateListItems(f, ex);
            }

            @Override
            protected List<Flight> doInBackground(Void... params) {

                List<Flight> flights = null;
                try {
                    flights = Utility.getTravelUS().pbtTracker();
                } catch (Exception e) {
                    ex = e;
                }
                return flights;
            }
        }.execute();
    }

    private void populateListItems(List<Flight> flights, Exception e) {

        try {
            final ListView lv = getListView();

            if (e != null) {
                getStatusTextView().setText(e.getMessage());
                return;
            }

            if (flights.size() > 0) {

                Collections.sort(flights);

                ArrayAdapter<Flight> flightArrayAdapter = new ArrayAdapter<Flight>(getParentActivity(),
                        R.layout.list_layout_flights, flights);
                setListAdapter(flightArrayAdapter);

                if (lv != null) {
                    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
                    lv.setSelector(android.R.color.darker_gray);
                }

            } else {
                ArrayAdapter<String> flightArrayAdapter = new ArrayAdapter<String>(getParentActivity(),
                        R.layout.list_layout_flights, new ArrayList<String>() {{
                    add(mActivity.getString(R.string.dashboardStatus_NoFlightsFound));
                }}
                );
                setListAdapter(flightArrayAdapter);
                if (lv != null)
                    lv.setChoiceMode(ListView.CHOICE_MODE_NONE);

            }
        } catch (Exception ex) {
            log(ex, getParentActivity());
        }
    }

    //</editor-fold>
    //<editor-fold desc="Interfaces">

    public interface OnFlightSelectionChangeListener {
        public void onFlightSelectionChanged(Flight flight);
    }

    //</editor-fold>
}
